package com.example.city_home_app.Activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;

import com.example.city_home_app.Bean.Down_Community;
import com.example.city_home_app.Model.ApiModel;
import com.example.city_home_app.R;
import com.example.city_home_app.RecyclerViewAdapter.Adapter_Community;
import com.example.city_home_app.RecyclerViewAdapter.Adapter_News;
import com.example.city_home_app.RetrofitListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CommunityAllActivity extends BaseActivity {
    @BindView(R.id.toolbar_community_allcommunity)
    Toolbar toolbarCommunityAllcommunity;
    @BindView(R.id.rec_community_all)
    RecyclerView recCommunityAll;
    @BindView(R.id.refresh_community_all)
    SwipeRefreshLayout refreshCommunityAll;

    private ApiModel model = new ApiModel();
    private Adapter_Community adapter_community;

    private RetrofitListener<List<Down_Community>> listRetrofitListener = new RetrofitListener<List<Down_Community>>() {
        @Override
        public void onResponse(List<Down_Community> listener) {
            adapter_community.setList(listener);
            adapter_community.notifyDataSetChanged();
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_community_allcommunity;
    }

    @Override
    protected void initViews() {
        super.initViews();
        ButterKnife.bind(this);
        recCommunityAll = (RecyclerView)findViewById(R.id.rec_community_all);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recCommunityAll.setLayoutManager(layoutManager);
        adapter_community = new Adapter_Community(this);
        recCommunityAll.setAdapter(adapter_community);
        recCommunityAll.setItemAnimator(new DefaultItemAnimator());

        refreshCommunityAll = (SwipeRefreshLayout) findViewById(R.id.refresh_community_all);
        refreshCommunityAll.setColorSchemeColors(0xff49afcd);
        refreshCommunityAll.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });

        showToast();
    }


    @Override
    protected void initData() {
        super.initData();
        model.getcommunitylist(listRetrofitListener);
    }

    private void refresh() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        model.getcommunitylist(listRetrofitListener);
                        refreshCommunityAll.setRefreshing(false);
                    }
                });
            }
        }).start();
    }

    private void showToast() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        toolbarCommunityAllcommunity = (Toolbar) findViewById(R.id.toolbar_community_allcommunity);

        setSupportActionBar(toolbarCommunityAllcommunity);

        toolbarCommunityAllcommunity.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setSupportActionBar(toolbarCommunityAllcommunity);
    }

}
